/*
 * 作者: 侯晓宇
 * 时间: 2017-01-11 
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	var appInfo = require("app/AppInfo");
	var c = require("app/utils/CommonUtils");
	var loadingUtils = require("app/utils/LoadingUtils");
	var tip = require("app/utils/TipUtils");
	return {
		post: function(url, body, callback_func) {
			return new Promise(function(resolve, reject) {
				loadingUtils.openloading("数据加载", "稍后片刻");
				
				if (body == null) {
					body = {};
				} 
				
				body.systemType = api.systemType;
				body.appid = appInfo.appid;
				
				c.debug("url", url);
				c.debug("body", body);
				api.ajax({
					url: url,
					method: 'post',
					data: {
						body: body
					},
					headers:{
						"Content-Type":"application/json;charset=UTF-8"
					},
					timeout: 20,
					cache: true
				}, function(ret, err) {

					loadingUtils.closeloading();
					if (ret) {
						c.debug("resp", ret);
						if (ret.status == "500") {
							tip.error();
						}

						if (!c.isNull(callback_func)) {
							callback_func(ret);
						} else {
							resolve(ret);
						}

					} else {
						c.debug("err", err);
						if (err.code == 1) { //超时
							var resp = {};
							resp.status = "504";
							tip.show(err.msg);
							if (!c.isNull(callback_func)) {
								callback_func(resp);
							} else {
								resolve(resp);
							}
						} else if (err.code == 0) { //网络不可用，服务器宕机或者app没有网络
							var resp = {};
							resp.status = "503";
							tip.show(err.msg);
							if (!c.isNull(callback_func)) {
								callback_func(resp);
							} else {
								resolve(resp);
							}
						}
					}
				});
			});
		},


		uploadImg: function(filepath, imgService, folder, callback_func) {
				return new Promise(function(resolve, reject) {
					//useLocalService
					var imghandler = '';
					if (c.isNull(imgService)) {
						imgService = 1;
					}
					if (imgService == 0) { //本地图片处理服务
						imghandler = 'native';
					} else if (imgService == 1) { //qiniu图片处理服务
						imghandler = 'qiniu';
					} else if (imgService == 2) { //阿里云图片处理服务
						imghandler = 'aliyun';
					}

					if(c.isNullStr(folder)){
						folder = "default";
					}
					var url = appInfo.serverbase + '/basic/' + imghandler + '/upload_img/folder/'+folder;
					c.debug("in uploadImg: url",url);
					loadingUtils.openloading("正在上传", "稍后片刻");
					api.ajax({
						url: url,
						method: 'post',
						timeout: 60,
						data: {
							files: {
								file: filepath
							}
						}
					}, function(ret, err) {

						loadingUtils.closeloading();
						if (ret) {
							c.debug("resp", ret);
							if (ret.status == "500") {
								tip.error();
							}
							if (!c.isNull(callback_func)) {
								callback_func(ret);
							} else {
								resolve(ret);
							}

						} else {
							c.debug("err", err);
							if (err.code == 1) { //超时
								var resp = {};
								resp.status = "504";
								tip.show(err.msg);

								if (!c.isNull(callback_func)) {
									callback_func(resp);
								} else {
									resolve(resp);
								}

							} else if (err.code == 0) { //网络不可用，服务器宕机或者app没有网络
								var resp = {};
								resp.status = "503";
								tip.show(err.msg);

								if (!c.isNull(callback_func)) {
									callback_func(resp);
								} else {
									resolve(resp);
								}
							}
						}

					});
				});
			}, //uploadImg -end
			
			uploadAudio: function(filepath, fileService, folder, callback_func) {
				return new Promise(function(resolve, reject) {
					//useLocalService
					var handler = '';
					if (c.isNull(fileService)) {
						fileService = 1;
					}
					if (fileService == 0) { //本地处理服务
						handler = 'native';
					} else if (fileService == 1) { //qiniu处理服务
						handler = 'qiniu';
					} else if (fileService == 2) { //阿里云处理服务
						handler = 'aliyun';
					}

					if(c.isNullStr(folder)){
						folder = "default";
					}
					var url = appInfo.serverbase + '/basic/' + handler + '/upload_audio/folder/'+folder;
//					var url = appInfo.serverbase + '/basic/' + handler + '/upload_audio/folder/'+folder;
					c.debug("url",url);
					loadingUtils.openloading("正在上传", "稍后片刻");
					api.ajax({
						url: url,
						method: 'post',
						timeout: 60,
						data: {
							files: {
								file: filepath
							}
						}
					}, function(ret, err) {

						loadingUtils.closeloading();
						if (ret) {
							c.debug("resp", ret);
							if (ret.status == "500") {
								tip.error();
							}
							if (!c.isNull(callback_func)) {
								callback_func(ret);
							} else {
								resolve(ret);
							}

						} else {
							c.debug("err", err);
							if (err.code == 1) { //超时
								var resp = {};
								resp.status = "504";
								tip.show(err.msg);

								if (!c.isNull(callback_func)) {
									callback_func(resp);
								} else {
									resolve(resp);
								}

							} else if (err.code == 0) { //网络不可用，服务器宕机或者app没有网络
								var resp = {};
								resp.status = "503";
								tip.show(err.msg);

								if (!c.isNull(callback_func)) {
									callback_func(resp);
								} else {
									resolve(resp);
								}
							}
						}

					});
				});
			}, //uploadAudio -end
			uploadPPT: function(filepath, service, folder, callback_func) {
				return new Promise(function(resolve, reject) {
					//useLocalService
					var imghandler = '';
					if (c.isNull(service)) {
						service = 0;
					}
					if (service == 0) { //本地图片处理服务
						handler = 'native';
					} else if (service == 1) { //qiniu图片处理服务
						handler = 'qiniu';
					} else if (service == 2) { //阿里云图片处理服务
						handler = 'aliyun';
					}

					if(c.isNullStr(folder)){
						folder = "default";
					}
					var url = appInfo.serverbase + '/basic/' + handler + '/upload_ppt/folder/'+folder;
					c.debug("in uploadPPT: url",url);
					loadingUtils.openloading("正在上传", "稍后片刻");
					api.ajax({
						url: url,
						method: 'post',
						timeout: 60,
						data: {
							files: {
								file: filepath
							}
						}
					}, function(ret, err) {

						loadingUtils.closeloading();
						if (ret) {
							c.debug("resp", ret);
							if (ret.status == "500") {
								tip.error();
							}
							if (!c.isNull(callback_func)) {
								callback_func(ret);
							} else {
								resolve(ret);
							}

						} else {
							c.debug("err", err);
							if (err.code == 1) { //超时
								var resp = {};
								resp.status = "504";
								tip.show(err.msg);

								if (!c.isNull(callback_func)) {
									callback_func(resp);
								} else {
									resolve(resp);
								}

							} else if (err.code == 0) { //网络不可用，服务器宕机或者app没有网络
								var resp = {};
								resp.status = "503";
								tip.show(err.msg);

								if (!c.isNull(callback_func)) {
									callback_func(resp);
								} else {
									resolve(resp);
								}
							}
						}

					});
				});
			}, //uploadPPT -end
			
			
	}; //return - end
	
	
	
});